package com.util;

/**
 * @author Diego Iacono
 * 
 */
public class Validator
{
   /**
    * Validate if the string passed is empty.
    * 
    * @param str String
    * @return boolean true if it's empty or false otherwise
    * 
    */
   public static boolean isNullOrEmpty(String str)
   {
      boolean rv = false;
      if (str == null || str.trim().length() == 0)
      {
         rv = true;
      }
      return rv;
   }

   /**
    * Add comma separator if necessary.
    * 
    * @param sb
    *           StringBuffer
    * @return StringBuffer
    */
   public static StringBuffer addCommaSprtrIfApply(StringBuffer sb)
   {
      StringBuffer rv = null;
      if (sb != null)
      {
         rv = sb;
      }
      if (rv.length() > 0)
      {
         rv.append(",\n");
      }
      return rv;
   }

   /**
    * Validate if array is null or empty.
    * 
    * @param objArry
    *           Object[]
    * @return boolean true if object array is null or empty only
    * 
    */
   public static boolean isNullOrEmpty(Object[] objArry)
   {
      boolean rv = false;
      if (objArry == null || objArry.length == 0)
      {
         rv = true;
      }
      return rv;
   }

   /**
    * Equals comparison for array objects.
    * 
    * @param objArry1
    *           Object[]
    * @param objArry2
    *           Object[]
    * @return boolean true if both object are equals only
    * 
    */
   public static boolean isEquals(Object[] objArry1, Object[] objArry2)
   {
      boolean rv = false;
      if (objArry1 == null && objArry2 == null)
      {
         rv = true;
      }
      else if (objArry1 == objArry2)
      {
         rv = true;
      }
      else if (objArry1 != null && objArry2 != null && objArry1.length == objArry2.length)
      {
         for (int i = 0; i < objArry1.length; i++)
         {
            Object obj1 = objArry1[i];
            Object obj2 = objArry2[i];
            if (obj1 == null && obj2 == null)
            {
               rv = true;
            }
            else if (obj1 != null && obj2 != null)
            {
               rv = obj1.equals(obj2);
            }
            else
            {
               rv = false;
            }
            if (!rv)
            {
               break;
            }
         }
      }
      return rv;
   }

   /**
    * Validate the Phone number format expected format value: 9999-999-9999.
    * 
    * @param number
    *           String
    * @return true/false
    */
   public static boolean validatePhoneNumberFormat(String number)
   {
      boolean valid = false;

      // Regular expression with expected format
      String phoneNumberPattern = "(\\d-)?(\\d{4}-)?\\d{3}-\\d{4}";
      if (!number.matches(phoneNumberPattern))
      {
         valid = true;
      }
      return valid;
   }

   /**
    * Email method validator
    * 
    * @param email
    * @return String valid result
    */
   public static String validateEmailFormat(String email)
   {

      if (!((email == null) || (email.equals(""))))
      {

         // checking @ Symbol
         int index_at = email.indexOf('@');
         if (index_at < 0)
         {
            return ConstantMessageSource.VALID_EMAIL_MESSAGE_AT_MISSING;
         }
         else
         {
            if (email.indexOf('@') != email.lastIndexOf('@') && email.indexOf('@') > -1)
            {
               return ConstantMessageSource.VALID_EMAIL_MESSAGE_AT_DUPLICATED;
            }
         }

         // checking dot after @ symbol
         int index_p = email.indexOf('.', index_at);
         if (index_p < 0)
         {
            return ConstantMessageSource.VALID_EMAIL_MESSAGE_DOT_MISSING;
         }

         // checking for characters prior to @ and between @ and period(.)
         if ((index_at == 0) || (index_p < (index_at + 2)))
         {
            return ConstantMessageSource.VALID_EMAIL_MESSAGE_ALL_INVALID;
         }

         // checking for domain chars.
         // condition: atleast two characters.
         // after last period('.').
         // maximum of 3 chars was taken off since there's no EAT refrence to it
         // and
         // we cannot control how domain names are created.
         int lastIndex = email.lastIndexOf('.');
         String lastSubStr = email.substring(lastIndex + 1);
         if ((lastIndex > -1) && (lastSubStr.length() < 2))
         {
            return ConstantMessageSource.VALID_EMAIL_MESSAGE_DOMAIN_INVALID;
         }

         // check if email address is not empty and contains an @ sign
         if (email != null && !email.trim().equals("") && email.indexOf("@") >= 1)
         {
            String dnsAddress = email.substring(email.indexOf("@") + 1);
            int lastDotIdx = dnsAddress.lastIndexOf(".");
            if (lastDotIdx >= 0)
            {
               String comp = dnsAddress.substring(0, lastDotIdx);

               // company must have at least one character
               if (comp.length() >= 1)
               {
                  String dom = dnsAddress.substring(lastDotIdx + 1);

                  // domain must have at least two characters
                  if (!(dom.length() >= 2))
                  {
                     return ConstantMessageSource.VALID_EMAIL_MESSAGE_DOMAIN_INVALID;
                  }

               }
            }
         }
      }

      return null;
   }
}
